package xin.cms.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import xin.cms.model.Notice;
import xin.cms.request.NoticePageRequest;
import xin.cms.service.NoticeService;
import xin.common.http.response.PageResponse;
import xin.common.http.response.Response;

/**
 * <pre>
 * 公告管理
 * </pre>
 *
 * @author lixin_ma@outlook.com
 * @version 1.0
 * @since 2018/8/9
 */
@RestController
@RequestMapping("notice")
@Api(value = "xin-cms", description = "公告管理", position = 3)
@Slf4j
public class NoticeController {

    @Autowired
    private NoticeService service;

    @ApiOperation(value = "公告管理" , notes = "公告管理",response = PageResponse.class )
    @GetMapping("pageList")
    public PageResponse noticeList(NoticePageRequest request){
        return service.findPageList(request);
    }

    @ApiOperation(value = "添加公告" , notes = "添加公告",response = Response.class )
    @PutMapping
    public Response putNotice(@RequestBody Notice notice){
        return editNotice(notice);
    }

    @ApiOperation(value = "编辑公告" , notes = "编辑公告",response = Response.class )
    @PostMapping
    public Response postNotice(@RequestBody Notice notice){
        return editNotice(notice);
    }

    @ApiOperation(value = "查看公告" , notes = "查看公告",response = Response.class )
    @GetMapping("{noticeId}")
    public Response getNotice(@PathVariable String noticeId){
        Response<Notice> response = new Response();
        response.setSuccess(Boolean.TRUE);
        response.setData(service.findOne(noticeId));
        return response;
    }

    private Response editNotice(Notice notice){
        Response<Notice> response = new Response();
        response.setSuccess(Boolean.TRUE);
        response.setData(service.save(notice));
        return response;
    }

}
